package com.meituan.mapper;

import com.meituan.model.OrderEntity;
import com.meituan.provider.OrderProvider;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.data.relational.core.sql.In;

import java.util.List;

public interface OrderMapper {

    @Select("select `order`.*,shop_name,shop_img from `order`,shop where user_id =#{userId} and shop.shop_id = `order`.shop_id")
    @Results({
            @Result(property = "orderId",column = "order_id"),
            @Result(property = "orderDetailList",column = "order_id",
                    many = @Many(select = "com.meituan.mapper.OrderDetailMapper.findById",fetchType = FetchType.EAGER))
    })
    List<OrderEntity> findAllByUserId(String userId);

    @Select("select * from `order` where shop_id = #{shopId} ")
    @Results({
            @Result(property = "orderId",column = "order_id"),
            @Result(property = "orderDetailList",column = "order_id",
                    many = @Many(select = "com.meituan.mapper.OrderDetailMapper.findById",fetchType = FetchType.EAGER)),

    })
    List<OrderEntity> findAllByShopId(String shopId);

    @Select("select `order`.*,shop_name,shop_img from `order`,shop where order_id =#{orderId} and shop.shop_id = `order`.shop_id")
    @Results({
            @Result(property = "orderId",column = "order_id"),
            @Result(property = "orderDetailList",column = "order_id",
                    many = @Many(select = "com.meituan.mapper.OrderDetailMapper.findById",fetchType = FetchType.EAGER))
    })
    OrderEntity findById(String orderId);

//    @Insert("insert into `order`(user_id,shop_id,receiving_name,receiving_telephone,receiving_address,remarks,shipping_fee,order_amount,create_time) values(#{userId},#{shopId},#{receivingName},#{receivingTelephone},#{receivingAddress},#{remarks},#{shippingFee},#{orderAmount},#{createTime})")
    @Options(useGeneratedKeys = true,keyProperty = "orderId",keyColumn = "order_id")
    @InsertProvider(type = OrderProvider.class,method = "insertProvider")
    Integer save(OrderEntity orderEntity);

//    @Update("update `order` set is_over=#{isOver} where order_id = #{orderId}")
    @UpdateProvider(type = OrderProvider.class,method = "updateOver")
    Integer updateIsOver(String orderId);

//    @Update("update `order` set is_pay=#{isPay} where order_id = #{orderId}")
    @UpdateProvider(type = OrderProvider.class,method = "updatePay")
    Integer updateIsPay( String orderId);

    @UpdateProvider(type = OrderProvider.class,method = "updateEvaluate")
    Integer updateIsEvaluate( String orderId);

    @Update("update `order` set is_delete = 1 where order_id=#{orderId}")
    Integer updateIsDelete(String orderId);

    @Select("select shop_id from `order` where order_id =#{orderId}")
    String findShopId(String orderId);


}
